<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$_SESSION['plugin_name']='jy_creditshop';
	if($_SESSION['plugin_name']){
		$params['plugin_name']=$_SESSION['plugin_name'];
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('core_account_sms',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'关闭','1'=>'启用');
	//$return['type']=array('1'=>'阿里通信短信','2'=>'聚合短信');
	$htmls=array(
			'type'=>'list',
			'tabs'=>array(
				array('op'=>'list','title'=>'短信模板管理'),
				array('op'=>'edit','title'=>'添加模板'),
			),
			'list'=>array('data'=>'list','list'=>array(
				array('name'=>'title','title'=>'模板名称','type'=>'string'),
				array('name'=>'smstplid','title'=>'模板ID','type'=>'string'),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				
			),
			),
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){	
	if($_RQ['id']){
		$item=pdo_get('core_account_sms',array('id'=>$_RQ['id']));
		$item['data']=json_decode($item['data'],true);
	}else{
		$item=array();
		$item['plugin_name']=$_SESSION['plugin_name'];
	}
	$item['typegroup']=$item['typegroup']?:1;
	$item['plugin_name']='jy_creditshop';
	$return['msgtpl_tags']=model($item['plugin_name'].'/model')->msgtpl_tags();
	$return['item']=$item;	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写名称','');
	}
	if(!$postdata['type']){
		//exi('请选择服务商','');
	}
	if(!$postdata['smstplid']){
		exi('请填写短信模板id','');
	}
	$data=array_fields(array('title','type','smstplid','status','content'),$postdata);	
	$data['data']=ijson_encode($data['data']);	
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_sms',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['plugin_name']=$_SESSION['plugin_name']?:'jy_creditshop';
		pdo_insert('core_account_sms',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='upcontent'){
	$return=array();
	$plugin_name=$_SESSION['plugin_name']?:'jy_creditshop';
	$smscontent='';
	if($_RQ['content']){
		$msgtpl_tags=model($plugin_name.'/model')->msgtpl_tags();
		$newtags=array();
		foreach($msgtpl_tags as $tv){
			foreach($tv['data'] as $dv){
				$newtags[]=$dv;
			}
		}
		$smscontent=cfc('account')->replaceTemplate($_RQ['content'],$newtags,true);
	}
	$return['smscontent']=$smscontent;	
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_sms',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_sms',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>